/**
 * 历史记录查看服务层
 */

// 模拟历史记录数据
const mockRecordsData = [
  {
    id: 1,
    operationType: 'login',
    operationDesc: '用户登录系统',
    detail: '通过移动端应用成功登录应急值守系统',
    operationTime: '2025-11-04 14:30:25',
    ipAddress: '192.168.1.100',
    device: 'iPhone 13 Pro',
    module: '系统登录',
    status: 'success'
  },
  {
    id: 2,
    operationType: 'view',
    operationDesc: '查看个人信息',
    detail: '访问个人信息管理页面，查看当前用户的基本资料',
    operationTime: '2025-11-04 14:32:10',
    ipAddress: '192.168.1.100',
    device: 'iPhone 13 Pro',
    module: '个人信息管理',
    status: 'success'
  },
  {
    id: 3,
    operationType: 'edit',
    operationDesc: '修改个人信息',
    detail: '更新了个人简介和联系方式信息',
    operationTime: '2025-11-04 14:35:42',
    ipAddress: '192.168.1.100',
    device: 'iPhone 13 Pro',
    module: '个人信息管理',
    status: 'success'
  },
  {
    id: 4,
    operationType: 'view',
    operationDesc: '查看通知设置',
    detail: '访问通知偏好设置页面，查看当前的通知配置',
    operationTime: '2025-11-04 14:38:15',
    ipAddress: '192.168.1.100',
    device: 'iPhone 13 Pro',
    module: '通知偏好设置',
    status: 'success'
  },
  {
    id: 5,
    operationType: 'edit',
    operationDesc: '修改通知设置',
    detail: '调整了系统通知和应急预警的接收方式',
    operationTime: '2025-11-04 14:40:28',
    ipAddress: '192.168.1.100',
    device: 'iPhone 13 Pro',
    module: '通知偏好设置',
    status: 'success'
  },
  {
    id: 6,
    operationType: 'view',
    operationDesc: '查看突发事件',
    detail: '访问突发事件查询模块，查询近期的突发事件信息',
    operationTime: '2025-11-04 10:15:33',
    ipAddress: '192.168.1.100',
    device: 'iPhone 13 Pro',
    module: '突发事件查询',
    status: 'success'
  },
  {
    id: 7,
    operationType: 'export',
    operationDesc: '导出事件报告',
    detail: '导出了突发事件的详细报告，文件格式为PDF',
    operationTime: '2025-11-04 10:20:45',
    ipAddress: '192.168.1.100',
    device: 'iPhone 13 Pro',
    module: '突发事件查询',
    status: 'success'
  },
  {
    id: 8,
    operationType: 'create',
    operationDesc: '创建事件记录',
    detail: '新建了一条突发公共卫生事件的应急响应记录',
    operationTime: '2025-11-03 16:45:20',
    ipAddress: '192.168.1.105',
    device: 'Huawei Mate 40',
    module: '应急响应',
    status: 'success'
  },
  {
    id: 9,
    operationType: 'edit',
    operationDesc: '更新事件状态',
    detail: '将事件状态从"进行中"更新为"已处理"',
    operationTime: '2025-11-03 17:30:12',
    ipAddress: '192.168.1.105',
    device: 'Huawei Mate 40',
    module: '应急响应',
    status: 'success'
  },
  {
    id: 10,
    operationType: 'view',
    operationDesc: '查看历史记录',
    detail: '访问历史记录查看页面，查询过往操作记录',
    operationTime: '2025-11-03 15:25:08',
    ipAddress: '192.168.1.105',
    device: 'Huawei Mate 40',
    module: '历史记录查看',
    status: 'success'
  },
  {
    id: 11,
    operationType: 'export',
    operationDesc: '导出历史记录',
    detail: '导出了近7天的操作历史记录，文件格式为CSV',
    operationTime: '2025-11-03 15:28:30',
    ipAddress: '192.168.1.105',
    device: 'Huawei Mate 40',
    module: '历史记录查看',
    status: 'success'
  },
  {
    id: 12,
    operationType: 'login',
    operationDesc: '用户登录系统',
    detail: '通过移动端应用成功登录应急值守系统',
    operationTime: '2025-11-03 09:00:15',
    ipAddress: '192.168.1.105',
    device: 'Huawei Mate 40',
    module: '系统登录',
    status: 'success'
  },
  {
    id: 13,
    operationType: 'view',
    operationDesc: '查看系统主题',
    detail: '访问系统主题选择页面，预览不同的主题样式',
    operationTime: '2025-11-02 14:20:45',
    ipAddress: '192.168.1.108',
    device: 'Xiaomi 12',
    module: '系统主题选择',
    status: 'success'
  },
  {
    id: 14,
    operationType: 'edit',
    operationDesc: '切换系统主题',
    detail: '将系统主题从浅色模式切换为深色模式',
    operationTime: '2025-11-02 14:22:10',
    ipAddress: '192.168.1.108',
    device: 'Xiaomi 12',
    module: '系统主题选择',
    status: 'success'
  },
  {
    id: 15,
    operationType: 'delete',
    operationDesc: '删除过期记录',
    detail: '清理了90天前的过期操作记录',
    operationTime: '2025-11-02 11:30:00',
    ipAddress: '192.168.1.108',
    device: 'Xiaomi 12',
    module: '数据维护',
    status: 'success'
  },
  {
    id: 16,
    operationType: 'login',
    operationDesc: '用户登录系统',
    detail: '通过移动端应用成功登录应急值守系统',
    operationTime: '2025-11-02 08:45:30',
    ipAddress: '192.168.1.108',
    device: 'Xiaomi 12',
    module: '系统登录',
    status: 'success'
  },
  {
    id: 17,
    operationType: 'view',
    operationDesc: '查看数据统计',
    detail: '访问数据分析页面，查看本月的应急事件统计',
    operationTime: '2025-11-01 16:15:20',
    ipAddress: '192.168.1.110',
    device: 'OPPO Find X5',
    module: '数据统计',
    status: 'success'
  },
  {
    id: 18,
    operationType: 'export',
    operationDesc: '导出统计报表',
    detail: '导出了本月的应急事件统计分析报表',
    operationTime: '2025-11-01 16:20:35',
    ipAddress: '192.168.1.110',
    device: 'OPPO Find X5',
    module: '数据统计',
    status: 'success'
  },
  {
    id: 19,
    operationType: 'create',
    operationDesc: '创建值班记录',
    detail: '新建了夜间值班的记录信息',
    operationTime: '2025-11-01 20:00:00',
    ipAddress: '192.168.1.110',
    device: 'OPPO Find X5',
    module: '值班管理',
    status: 'success'
  },
  {
    id: 20,
    operationType: 'login',
    operationDesc: '用户登录系统',
    detail: '通过移动端应用成功登录应急值守系统',
    operationTime: '2025-11-01 08:30:00',
    ipAddress: '192.168.1.110',
    device: 'OPPO Find X5',
    module: '系统登录',
    status: 'success'
  }
];

/**
 * 获取历史记录列表
 */
export function getHistoricalRecords() {
  return new Promise((resolve) => {
    // 模拟网络延迟
    setTimeout(() => {
      // 按时间倒序排列
      const sortedRecords = [...mockRecordsData].sort((a, b) => {
        return new Date(b.operationTime) - new Date(a.operationTime);
      });
      resolve(sortedRecords);
    }, 500);
  });
}

/**
 * 获取统计信息
 */
export function getStatistics() {
  return new Promise((resolve) => {
    setTimeout(() => {
      const today = new Date().toISOString().split('T')[0];
      const todayRecords = mockRecordsData.filter(record => 
        record.operationTime.startsWith(today)
      );
      
      // 获取最后登录时间
      const loginRecords = mockRecordsData
        .filter(record => record.operationType === 'login')
        .sort((a, b) => new Date(b.operationTime) - new Date(a.operationTime));
      
      const lastLoginTime = loginRecords.length > 0 
        ? loginRecords[0].operationTime.split(' ')[1]
        : '--';

      resolve({
        totalRecords: mockRecordsData.length,
        todayRecords: todayRecords.length,
        lastLoginTime: lastLoginTime
      });
    }, 300);
  });
}

/**
 * 导出历史记录
 */
export function exportRecords(records) {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 生成CSV内容
      const headers = ['序号', '操作类型', '操作描述', '详细信息', '操作时间', 'IP地址', '设备信息', '操作模块', '状态'];
      const typeMap = {
        'login': '登录',
        'view': '查看',
        'edit': '编辑',
        'export': '导出',
        'delete': '删除',
        'create': '创建'
      };
      
      let csvContent = '\uFEFF'; // UTF-8 BOM
      csvContent += headers.join(',') + '\n';
      
      records.forEach((record, index) => {
        const row = [
          index + 1,
          typeMap[record.operationType] || record.operationType,
          `"${record.operationDesc}"`,
          `"${record.detail}"`,
          record.operationTime,
          record.ipAddress,
          record.device,
          record.module,
          record.status === 'success' ? '成功' : '失败'
        ];
        csvContent += row.join(',') + '\n';
      });
      
      const filename = `历史记录_${new Date().toISOString().split('T')[0]}.csv`;
      
      resolve({
        data: csvContent,
        filename: filename
      });
    }, 1000);
  });
}

